package yl.hs.bmipfull.service.laboratorydata.imple;


import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import yl.hs.bmipfull.daos.laboratorydata.ILabRmInspectionRecordDao;
import yl.hs.bmipfull.pojo.laboratorydata.LabRmInspectionRecordEntity;
import yl.hs.bmipfull.pojo.laboratorydata.LabRmSandReportItemEntity;
import yl.hs.bmipfull.pojo.laboratorydata.LabStoneInspectionItemsEntity;
import yl.hs.bmipfull.pojo.laboratorydata.LabStoneInspectionScreeningEntity;
import yl.hs.bmipfull.service.laboratorydata.ILabRmInspectionRecordService;
import yl.hs.bmipfull.service.laboratorydata.ILabStoneInspectionItemsService;
import yl.hs.bmipfull.service.laboratorydata.ILabStoneInspectionScreeningService;
import yl.hs.bmipfull.utils.DataState;
import yl.hs.bmipfull.utils.SearchFilterEngine;
import yl.hs.bmipfull.utils.Tl;
import yl.hs.bmipfull.utils.datastate.ActionType;
import yl.hs.bmipfull.viewmodels.PageQuery;
import yl.hs.bmipfull.viewmodels.PageResult;

import java.util.List;
import java.util.Map;

/**
 * @author 谢东
 * @apiNote 原材实验记录——基础信息服务层接口实现
 * @since 2022年06月24日 09时56分05秒 <br/>
 */
@Service
public class LabRmInspectionRecordServiceImpl extends LaboratorydataBaseService implements ILabRmInspectionRecordService {

    @Autowired
    ILabRmInspectionRecordDao dao;
    @Autowired
    ILabStoneInspectionItemsService inspectionItemsService;
    @Autowired
    ILabStoneInspectionScreeningService stoneInspectionScreeningService;

    @Override
    public List<LabRmInspectionRecordEntity> queryList(PageQuery pageQuery) throws Exception {
        pageQuery.checkSortSides(LabRmInspectionRecordEntity.class);
        var filters = SearchFilterEngine.generateQueryList(pageQuery.getFilters(), LabRmInspectionRecordEntity.class);
        return dao.queryPage(pageQuery.getQuery(),filters);
    }

    @Override
    public PageResult<LabRmInspectionRecordEntity> queryPage(PageQuery pageQuery, int rowNum, int page) throws Exception {
        pageQuery.checkSortSides(LabRmInspectionRecordEntity.class);
        pageQuery.getQuery().put("querySkip", (page - 1) * rowNum);
        pageQuery.getQuery().put("pageSize", rowNum);
        PageResult<LabRmInspectionRecordEntity> result = new PageResult(rowNum);
        var filters = SearchFilterEngine.generateQueryList(pageQuery.getFilters(), LabRmInspectionRecordEntity.class);
        result.setRecords(dao.queryCountByCondition(pageQuery.getQuery(),filters));
        result.setPage(page);
        result.setData(dao.queryPage(pageQuery.getQuery(),filters));
        return result;
    }


    @Override
    public LabRmInspectionRecordEntity findBySn(String sn) {
        return dao.findBySN(sn);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer saveLabRmInspectionRecord(Map<String, Object> model, String sn) throws Exception {
        Integer result = 0;
        LabRmInspectionRecordEntity entity = null;
        ObjectMapper objectMapper = new ObjectMapper();
        if (Tl.isEmpty(sn)) {
            //执行保存
            entity = new LabRmInspectionRecordEntity();
            Tl.getChangeLog(entity, model);
            entity.setPYFieldValue();
            entity.setSn(queryNewSN(LabRmInspectionRecordEntity.class));
            var subData=(Map)model.get("subData");
            //var stoneScreening=model.get("stoneScreening");
            var graveDataEntity=new LabStoneInspectionItemsEntity();
            Tl.getChangeLog(graveDataEntity,subData);
            //graveDataEntity=objectMapper.convertValue(subData, LabStoneInspectionItemsEntity.class);
            //var stoneScreeningEntity=objectMapper.convertValue(stoneScreening, LabStoneInspectionScreeningEntity.class);
            graveDataEntity.setInspectionSN(entity.getSn());
            //stoneScreeningEntity.setReportSN(entity.getSn());
            inspectionItemsService.saveLabStoneInspectionItemsOnPojo(graveDataEntity,"");
            //stoneInspectionScreeningService.saveLabStoneInspectionScreening(stoneScreeningEntity.toMap(),"");
            result = dao.insertNew(entity);
            if (result > 0) {
                writeLog(ActionType.CREATE, entity.getSn(), "插入原材实验记录——基础信息信息", Tl.toJson(entity), LabRmInspectionRecordEntity.class);
            }

        } else {
            //执行修改
            entity = findBySn(sn);
            Tl.getChangeLog(entity, model);
            entity.setPYFieldValue();
            result = dao.update(entity);
            inspectionItemsService.deleteByInspectionSN(entity.getSn());
            var subData=model.get("subData");
            var graveDataEntity=objectMapper.convertValue(subData, LabStoneInspectionItemsEntity.class);
            graveDataEntity.setInspectionSN(entity.getSn());
            inspectionItemsService.saveLabStoneInspectionItemsOnPojo(graveDataEntity,"");
            if (result > 0) {
                writeLog(ActionType.UPDATE, sn, "更新原材实验记录——基础信息信息", Tl.toJson(entity), LabRmInspectionRecordEntity.class);
            }
        }

        return result;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer delete(String[] sn) {
        Integer result = dao.changeStateBySN(sn, DataState.DELETED.code);
        if (result > 0) {
            writeLog(ActionType.DELETE, sn, LabRmInspectionRecordEntity.class);
        }
        return result;
    }
}